<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属阶段" prop="stageName">
        <el-select v-model="queryParams.stageName" placeholder="请选择所属阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务要求" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务要求" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_requirements"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskCategory">
        <el-select v-model="queryParams.taskCategory" placeholder="请选择任务类型" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="taskPriority">
        <el-select v-model="queryParams.taskPriority" placeholder="请选择优先级" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_priority"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:my_stage_tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:my_stage_tasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stage_tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属阶段" align="center" prop="stageName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_stage" :value="scope.row.stageName"/>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" align="center" prop="taskCode" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务要求" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_requirements" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" align="center" prop="taskCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_type" :value="scope.row.taskCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="taskPriority">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_priority" :value="scope.row.taskPriority"/>
        </template>
      </el-table-column>
      <el-table-column label="任务描述" align="center" prop="taskDescription" />
      <el-table-column label="完成情况" prop="submitStatus" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.submitStatus === null || scope.row.submitStatus === 0">未提交</span>
          <span v-else>已提交</span>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="attachment">
        <template slot-scope="scope">
          <a v-if="scope.row.attachment" :href="scope.row.attachment" target="_blank">
            <image-preview :src="fileIcon" alt="File" :width="50" :height="50"/>
          </a>
          <span v-else>未上传</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.submitStatus === '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:my_stage_tasks:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.submitStatus === null || scope.row.submitStatus === '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSatgeTaskSubmit(scope.row)"
            v-hasPermi="['teaching:my_stage_tasks:submit']"
          >提交</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 提交或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务ID" prop="id" style="display: none;">
          <el-input v-model="form.id"/>
        </el-form-item>
        <el-form-item label="提交状态" prop="submitStatus" style="display: none;">
          <el-input v-model="form.submitStatus"/>
        </el-form-item>
        <el-form-item label="附件" prop="attachment">
          <file-upload v-model="form.attachment"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMy_Stage_tasks, listMy_Stage_tasks, updateMy_Stage_tasks} from "@/api/teaching/my_stage_tasks";
import fileIcon from '@/assets/images/file.png';


export default {
  name: "Stage_tasks_submit",
  dicts: ['sys_stage', 'ya_task_requirements', 'ya_task_type', 'ya_task_priority'],
  data() {
    return {
      // 文件图片
      fileIcon: fileIcon,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进度弹窗相关
      openProgress: false,
      classesList: [],      // 班级列表
      studentTasks: [],     // 学生任务列表
      selectedClassId: null, // 选中的班级ID
      taskId: null,         // 当前任务ID
      // 任务表格数据
      stage_tasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stageName: null,
        taskCode: null,
        taskName: null,
        taskType: null,
        taskCategory: null,
        taskPriority: null,
        submitStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stageName: [
          { required: true, message: "所属阶段不能为空", trigger: "change" }
        ],
        taskCode: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务要求不能为空", trigger: "change" }
        ],
        taskCategory: [
          { required: true, message: "任务类型不能为空", trigger: "change" }
        ],
        taskPriority: [
          { required: true, message: "优先级不能为空", trigger: "change" }
        ],
        taskStatus: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        stageName: null,
        taskCode: null,
        taskName: null,
        taskType: null,
        taskCategory: null,
        taskPriority: null,
        taskStatus: null,
        taskDescription: null,
        attachment: null,
        submitStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询我的任务列表 */
    getList() {
      this.loading = true;
      listMy_Stage_tasks(this.queryParams).then(response => {
        this.stage_tasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.submitStatus === null) {
            addMy_Stage_tasks(this.form).then((response) => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else if (this.form.submitStatus === '0') {
            updateMy_Stage_tasks(this.form).then((response) => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateMy_Stage_tasks(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attachment = row.attachment
      const id = row.id || this.ids
      const submitStatus = row.submitStatus
      this.form.submitStatus = submitStatus;
      this.form.taskId = id;
      this.form.attachment = attachment;
      this.open = true;
      this.title = "修改提交";
    },
    /** 提交任務操作 */
    handleSatgeTaskSubmit(row) {
      this.reset();
      const id = row.id || this.ids
      const submitStatus = row.submitStatus
      this.form.submitStatus = submitStatus;
      this.form.taskId = id;
      this.open = true;
      this.title = "提交任务";
    },
  }
};
</script>
